package cn.lyn4ever.message.service;

import cn.lyn4ever.message.pojo.acceptmsg.MessageText;
import cn.lyn4ever.message.pojo.sendmsg.PicMessage;
import cn.lyn4ever.message.pojo.sendmsg.inmessage.InPicMessage;
import cn.lyn4ever.utils.ImageMerge;
import cn.lyn4ever.utils.RequestUtils;
import cn.lyn4ever.utils.XmlUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Author: 007
 * @Date: 2019/9/25 17:33
 * @Version: 1.0
 */
@Service
public class TextMessageService {
    /**
     * 创建用户头像,
     *
     * @param baseMessage
     * @return
     */
    public String createHeadimg(MessageText baseMessage) {

        String openid = baseMessage.getFromUserName();

        //拿到token
        String token = "";
        String userInfo = "";
        try {
            token = RequestUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4e55bed9ca605bb4&secret=991a4e8848048d011f0841971cdbc4c4", null);
            token = (String) JSON.parseObject(token).get("access_token");
            System.out.println("token:++++++++++++++++" + token);
            userInfo = RequestUtils.sendGet("http://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid=" + openid + "&lang=zh_CN", null);
            System.out.println("userinfo:" + userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String oldHeadImgStr = JSON.parseObject(userInfo).getString("headimgurl");
        System.out.println(oldHeadImgStr);

        ImageMerge imageMerge = new ImageMerge();


        //先拿到原本图片和水印图片
        BufferedImage oldHeadImg = imageMerge.getBufferedImageDestUrl(oldHeadImgStr);
        BufferedImage frontImg = null;
        try {
            frontImg = imageMerge.getBufferedImage("static/img/imageMerge/star.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }

        int w1 = oldHeadImg.getWidth();
        int h1 = oldHeadImg.getHeight();

        int w2 = frontImg.getWidth();
        int h2 = frontImg.getHeight();

        int x = w1 - w2;
        int y = h1 - h2;

        BufferedImage newHeadImg = null;
        try {
            newHeadImg = imageMerge.overlyingImage(oldHeadImg, frontImg, x, y, 1.0f);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String saveFilePath = "static/img/imageMerge/aaa.jpg";

        imageMerge.generateSaveFile(newHeadImg, saveFilePath);


//        RequestUtils.upload("https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+token+"&type=TYPE",saveFilePath);

        String result = "";
        try {
            result = RequestUtils.uploadMediaToWX("D://test2.jpg", "image", token);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("resyutl" + result);
        String newImg = JSON.parseObject(result).getString("media_id");
        System.out.println(
                newImg
        );


        PicMessage newMessage = new PicMessage();
        InPicMessage messagePic = new InPicMessage();
        messagePic.setMediaId(newImg);
        newMessage.setImage(messagePic);

        newMessage.setToUserName(baseMessage.getFromUserName());
        newMessage.setFromUserName(baseMessage.getToUserName());
        newMessage.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000L));
        newMessage.setMsgType("image");

        String xml = XmlUtils.beanToXml(newMessage);
        System.out.println(xml);

//        https:
//api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN


        return xml;
    }
}
